using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Helpers.Translations;
using FiltronASP.Helpers.CatalogueHelpers;
using System.Net;

namespace FiltronASP.Helpers.AjaxModel
{
    /// <summary>
    /// Klasa pomocnicza do generowania stringa z lista modeli.
    /// </summary>
    public static class machineModelHelperFiltron
    {
        /// <summary>
        /// Metoda generujaca string do comba na podstawie id marki.
        /// </summary>
        /// <param name="makeId">marka</param>
        /// <returns>string</returns>
        public static string GetModelComboString(int makeId, string languageName)
        {
            string comboString = translationHelper.getTranslation("_selectModel", languageName) + ",-1;";
            IFiltronModelDAO filtronModelDAO = new FiltronModelDAO();
            if (makeId > 0)
            {
                IQueryable<FiltronModel> filtronModels = filtronModelDAO.FindByMake(makeId);
                if (filtronModels != null)
                {
                    
                    foreach (FiltronModel model in filtronModels)
                    {
                        comboString += model.model_name;
                        comboString += ",";
                        comboString += model.model_id;
                        comboString += ";";
                    }   //foreach
                }   //if
            }   //if
            return comboString;
        }

        /// <summary>
        /// Metoda generujaca diva leftSearchSide do silnikow na podstawie id modelu.
        /// </summary>
        /// <param name="modelId">model</param>
        /// <returns>string</returns>
        public static string GetEngineDivString(int modelId, string languageName)
        {
            if (modelId.Equals(0))
            {
                return null;
            }
            if (languageName == "")
            {
                return null;
            }
            return Helpers.AjaxModel.EngineTableHelper.generateEngineTable(modelId, languageName);
        }

        /// <summary>
        /// Metoda generujaca diva leftSearchSide do silnikow.
        /// </summary>
        /// <returns>string</returns>
        public static string GetEngineDivString()
        {
            return String.Empty;
        }

        /// <summary>
        /// Metoda generująca listę marek na podstawie typu pojazdu. (vehicle)
        /// </summary>
        /// <param name="vehicleId">typ pojazdu</param>
        /// <returns>string</returns>
        public static string getBrandDropDownByVehicleId(int vehicleId)
        {
            string comboString = "--- wybierz producenta ---,-1;";
            IFiltronMakeDAO filtronMakesDAO = new FiltronMakeDAO();
            if (vehicleId > 0) {
                IQueryable<FiltronMake> filtronMakes = filtronMakesDAO.FindByVehicle(vehicleId);
                if (filtronMakes != null) {
                    comboString = String.Empty;
                    foreach (FiltronMake make in filtronMakes)
                    {
                        comboString += make.make_name;
                        comboString += ",";
                        comboString += make.make_id;
                        comboString += ";";
                    }
                }   //if
            }
            return comboString;
        }

        public static string GetFiltersByEngineAndModel(int engineId, int modelId, string languageName)
        {
            if (engineId.Equals(0))
            {
                return null;
            }
            if (modelId.Equals(0))
            {
                return null;
            }
            if (languageName == "")
            {
                return null;
            }

            return Helpers.AjaxModel.FiltersTableHelper.generateFiltersTable(engineId, modelId, languageName, 1);
        }

        public static string generateSingleFilter(int filterId, string languageName) { 
            string htmlToReturn = "";
            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            FiltronFilter filter = filtronFilterDAO.FindByPK(filterId);            
            htmlToReturn += "<div class=\"searchRightSideTop\" id=\"searchRightSideTop\">";
            htmlToReturn += "                <div id=\"searchRightSideTopLeft\">";
            htmlToReturn += "                    <h3>" + translationHelper.getTranslation("_filter", languageName) + " <span>" + FiltronHelper.GetFiltronFilterNumber(filter) + "</span></h3>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftDimensions\">";
            htmlToReturn += "                        <strong>" + translationHelper.getTranslation("_hlpSizes", languageName) + " (mm):</strong>";
            htmlToReturn += "                        <table>";
            string sizeG = String.Empty;
            string sizeF = String.Empty;
            if (filter.filter_dim_g_text != null) { sizeG = filter.filter_dim_g_text; } else { sizeG = Convert.ToString(SingleFilterHelper.parseFilterSizes(filter.filter_dim_g)); }
            if (filter.filter_dim_f_text != null) { sizeF = filter.filter_dim_f_text; } else { sizeF = Convert.ToString(SingleFilterHelper.parseFilterSizes(filter.filter_dim_f)); }

            htmlToReturn += "                            <tr><td>A: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_a) + "</span></td><td>B: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_b) + "</span></td><td>C: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_c) + "</span></td></tr>";
            htmlToReturn += "                            <tr><td>D: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_d) + "</span></td><td>E: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_e) + "</span></td><td>F: <span>" + sizeF + "</span></td></tr>";
            htmlToReturn += "                            <tr><td>G: <span>" + sizeG + "</span></td><td>H: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_h) + "</span></td></tr>";
            htmlToReturn += "                        </table>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftInfo\">";
            htmlToReturn += "                        <ul>";

            string byPassValve = translationHelper.getTranslation("_hlpNo", languageName);
            string antiDrain = translationHelper.getTranslation("_hlpNo", languageName);
            string antiSyphon = translationHelper.getTranslation("_hlpNo", languageName);
            if (filter.filter_by_pass_valve == true)
            {
                byPassValve = translationHelper.getTranslation("_hlpYes", languageName);
            }
            if (filter.filter_anti_drain_valve == true)
            {
                antiDrain = translationHelper.getTranslation("_hlpYes", languageName);
            }
            if (filter.filter_anti_siphon_valve == true)
            {
                antiSyphon = translationHelper.getTranslation("_hlpYes", languageName);
            }

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpByPassValve", languageName) + ": <span>{0}</span></li>", byPassValve);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpAntiDrain", languageName) + ": <span>{0}</span></li>", antiDrain);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpAntiSiphon", languageName) + ": <span>{0}</span></li>", antiSyphon);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpInstruction", languageName) + ": <span>{0}</span></li>", String.Empty);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpApplication", languageName) + ":<br/><span>{0}</span></li>", filter.filter_principle_applications);
            htmlToReturn += "                        </ul>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftCommands\">";
            htmlToReturn += "                        <img onclick=\"printFilterDescription()\" src=\"../../../Content/images/engine_printer.jpg\" alt=\"Printer\" /> <span onclick=\"printFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpPrint", languageName) + "</span>";
            htmlToReturn += "                        <img onclick=\"sendFilterDescription()\" src=\"../../../Content/images/engine_mail.jpg\" alt=\"Mail\" /> <span onclick=\"sendFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpSend", languageName) + "</span>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                </div>";

            string imgPath = @"http://wixeurope.com/Content/images/filters/Filtron/" + FiltronHelper.GetFiltronFilterNumber(filter) + ".jpg";
            string imgUrl = String.Empty;
            string imgUrlLarge = String.Empty;
            bool showZoom = true;
            var request = (HttpWebRequest)WebRequest.Create(imgPath);
            request.Method = "HEAD";
            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                imgUrl = imgPath;
                imgUrlLarge = @"http://wixeurope.com/Content/images/filters/Filtron/large/" + FiltronHelper.GetFiltronFilterNumber(filter) + ".jpg";
            }
            catch
            {
                IFiltronFilterMediaDAO filtronFilterMediaDAO = new FiltronFilterMediaDAO();
                FiltronFilterMedia filterMedia = filtronFilterMediaDAO.FindByPK(filter.filter_media_id);
                string orgFilterMedia = String.Empty;
                if (filterMedia != null)
                {
                    orgFilterMedia = filterMedia.media_name;
                    string RepImageName = FiltronHelper.GetFiltronFilterImage(orgFilterMedia);
                    imgUrl = String.Format(@"http://wixeurope.com/Content/images/filters/Filtron/{0}.jpg", RepImageName);
                }
                showZoom = false;
            }

            htmlToReturn += "                <div id=\"searchRightSideTopRightFiltron\">";
            htmlToReturn += "                    <img src=\""+ imgUrl +"\" alt=\"Example\" />";
            if (showZoom)
                htmlToReturn += "                    <a href=\""+ imgUrlLarge +"\" rel=\"lightbox\"><span>" + translationHelper.getTranslation("_hlpZoom", languageName) + " <img src=\"../../../Content/images/engine_magn.png\" alt=\"Zoom\" /></a></span>";
            htmlToReturn += "                </div>";
            htmlToReturn += "            </div>";
            return htmlToReturn;
        }

    }
}
